package com.xls.box2023.jinbi.tixianjilu;

import androidx.recyclerview.widget.RecyclerView;

import com.lzz.bottomencapsulation.consts.Consts;
import com.lzz.bottomencapsulation.net.RequestCallBack;
import com.xls.box2023.adapter.TiXianJiLuAdapter;
import com.xls.box2023.entity.PageEntity;
import com.xls.box2023.entity.TiXianJiLuEntity;
import com.xls.box2023.jinbi.JinBiNetWorkUtil;
import com.xls.box2023.mvp.BasePresenterImpl;
import com.xls.xrecyclerview.XRecyclerView;

import java.util.ArrayList;

/**
 * MVPPlugin
 */

public class TiXianJiLuPresenter extends BasePresenterImpl<TiXianJiLuContract.View> implements TiXianJiLuContract.Presenter{
    TiXianJiLuAdapter tiXianJiLuAdapter;
    ArrayList<TiXianJiLuEntity> tiXianJiLuDataList=new ArrayList<>();
    int pageSize= Consts.PageSize,pageNum=Consts.PageNum;
    @Override
    public RecyclerView.Adapter getTiXianJiLuAdapter() {
        if (tiXianJiLuAdapter==null){
            tiXianJiLuAdapter=new TiXianJiLuAdapter(context,tiXianJiLuDataList);
        }
        return tiXianJiLuAdapter;
    }


    @Override
    public void getSourceData() {
        super.getSourceData();
        JinBiNetWorkUtil.getInstance().getMyCoinsExchangeRecords(pageSize, pageNum, new RequestCallBack<PageEntity<TiXianJiLuEntity>>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {
                if (mView==null||context==null)return;
                mView.getListView().refreshComplete();
                mView.getListView().loadMoreComplete();
            }

            @Override
            public void onRequestSuccess(PageEntity<TiXianJiLuEntity> requestData) {
                if (mView==null||context==null||requestData==null)return;
                if (pageNum==Consts.PageNum){
                    tiXianJiLuDataList.clear();
                }
                if (requestData.getRecords()!=null){
                    tiXianJiLuDataList.addAll(requestData.getRecords());
                }
                if (tiXianJiLuDataList.size()>=requestData.getTotal()){
                    mView.getListView().setNoMore(true);
                }else {
                    mView.getListView().setNoMore(false);
                }
                tiXianJiLuAdapter.notifyDataSetChanged();
            }

            @Override
            public void onRequestfail(String errno, String errmsg) {

            }
        });
    }

    public XRecyclerView.LoadingListener getLoadingListener() {

        return new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                    pageNum=Consts.PageNum;
                    getSourceData();
            }

            @Override
            public void onLoadMore() {
                    pageNum=pageNum+1;
                    getSourceData();
            }
        };
    }
}
